package com.github.aloxc.plugin.restplus.component;

import com.github.aloxc.plugin.restplus.common.Consts;
import com.intellij.openapi.editor.colors.FontPreferences;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import java.awt.*;
/**
 *
 * @author leero
 */
public class VTree extends JTree {
    private SimpleAttributeSet keyAttr;
    private SimpleAttributeSet valueAttr;
    private SimpleAttributeSet normalAttr;
    private Color lineBackgroundColor;
    private String fontFamily ;
    /**
     * 是否实现行号，默认不显示
     */
    private boolean showLineNumber = false;
    private int fontSize = 14;
    public VTree(Color lineBackgroundColor) {
        super();
        init();
        this.lineBackgroundColor = lineBackgroundColor;

    }
    private void init(){
        this.keyAttr = new SimpleAttributeSet();
        this.valueAttr = new SimpleAttributeSet();
        this.normalAttr = new SimpleAttributeSet();
        FontPreferences fontPreferences = this.getFontPreferences();
        fontFamily = fontPreferences.getFontFamily();
        StyleConstants.setForeground(this.keyAttr, Consts.MAIN_COLOR);
        StyleConstants.setFontSize(this.keyAttr, fontSize);
        StyleConstants.setFontFamily(this.keyAttr, fontFamily);

        Color fc = new Color(171,15,58);
        StyleConstants.setForeground(this.normalAttr, Color.gray);
        StyleConstants.setFontSize(this.normalAttr, fontSize);
        StyleConstants.setFontFamily(this.normalAttr,fontFamily);

        StyleConstants.setForeground(this.valueAttr,fc);
        StyleConstants.setFontSize(this.valueAttr, fontSize);
        StyleConstants.setFontFamily(this.valueAttr, fontFamily);
    }


    @NotNull
    private final FontPreferences getFontPreferences() {
        return new FontPreferences();
    }

    public void setShowLineNumber(boolean isShow) {
        this.showLineNumber = isShow;
    }

    public boolean getShowLineNumber() {
        return this.showLineNumber;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (getShowLineNumber()) {
            drawLineNumber(g);
        }
    }

    protected void drawLineNumber(Graphics g) {
        // 绘制行号的背景色

        g.setColor(lineBackgroundColor);

        g.fillRect(0, 0, 30, getHeight());
        this.getRowCount();
        // 获得有多少行
        int rows = this.getRowCount();
        // 绘制行号的颜色
        g.setColor(Consts.MAIN_COLOR);
        Font font = new Font(FontPreferences.DEFAULT_FONT_NAME, Font.PLAIN, fontSize);
        g.setFont(font);
        for (int row = 0; row < rows; row++) {
            g.drawString((row + 1) + "", 2, getPositionY(row + 1));
        }
    }


    public int getFontSize() {
        return fontSize;
    }

    /**
     * 获得行号中y坐标的位置<br/>
     * 在计算的过程中，有一个比率值，该比率值是根据getY()的返回值之差决定的。
     *
     * @param row 第几行
     * @return 该行的y坐标位置
     */
    private int getPositionY(int row) {
        int y = 0;
        switch (getFontSize()) {
            case 12:
                y = (row * 20) - 6;
                break;
            case 14:
                y = (row * 22) - 6;
                break;
            case 16:
                y = (row * 26) - 8;
                break;
            case 18:
                y = (row * 29) - 10;
                break;
            case 20:
                y = (row * 31) - 11;
                break;
            case 22:
                y = (row * 34) - 12;
                break;
            default:

        }
        return y;
    }

    @Override
    public void setSize(Dimension d) {
        if(d.width<getParent().getSize().width){
            d.width=getParent().getSize().width;
        }
        d.width+=100;
        super.setSize(d);
    }

    @Override
    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

}
